function search(){
	var query=$('#searchBox').val();
	$.ajax({  
        type: "GET",  
        url: "get-recommend.do?q="+query,  
        success: recommendPro  
	     });
	return false;
}

function recommendPro(data){
	$('#content-box').html(data);
}
// history bean class
function hisBean(fund_name,price,date){
	this.fund_name=fund_name;
	this.price=price;
	this.date=date;
}

// transaction bean class
function tranBean(id,price,share,amount,status,fund_name,type,date,color){
	this.id=id;
	this.price=price;
	this.share=share;
	this.amount=amount;
	this.status=status;
	this.fund_name=fund_name;
	this.type=type;
	this.date=date;
	this.color=color;
}


function historyPro(xml){
	// load data
	hisArr=new Array();
	var index_load=0;
	$(xml).find('history').each(function(){
		var fund_name=$(this).find('fund_name').text();
		var price=$(this).find('price').text();
		var date=$(this).find('date').text();
		var his=new hisBean(fund_name,price,date);
		hisArr[index_load++]=his;		
	});
	
	// draw chart
	drawChart();
}


function drawChart(){
	 var data = new google.visualization.DataTable();
	  data.addColumn('string', 'type');
	  data.addColumn('date', 'Date');
	  data.addColumn('number', 'value');
	  
	  var len=hisArr.length;
	  for(var index=0;index<len;index++){
		  var his=hisArr[index];
		  var dArr=his.date.split("-");
	  data.addRows([
	    
	    [his.fund_name,new Date (dArr[0],dArr[1]-1,dArr[2]),parseFloat(his.price)]
	    ]);
	  }
	  var chart = new google.visualization.MotionChart(document.getElementById('chart'));
	  var options = {};
	  var dd=new Date();
	  var today=dd.getFullYear()+"-"+(dd.getMonth()+1)+"-"+dd.getDay();
	  options['state']='{"yAxisOption":"2","dimensions":{"iconDimensions":["dim0"]},"playDuration":15000,"xAxisOption":"_TIME","iconType":"LINE","xLambda":1,"yZoomedDataMin":0,"xZoomedDataMin":1296518400000.567,"orderedByX":false,"time":\"'+today+'\","sizeOption":"_UNISIZE","uniColorForNonSelected":false,"showTrails":false,"duration":{"timeUnit":"D","multiplier":1},"nonSelectedAlpha":0.4,"yLambda":1,"yZoomedIn":false,"xZoomedDataMax":1296604800000.567,"colorOption":"_UNICOLOR","xZoomedIn":false,"iconKeySettings":[],"orderedByY":false}';
	options['width'] = 500;
	options['height'] = 450;
	  chart.draw(data, options);
}

function researchFund(fundID){
	$('#content-box').html("");
	$('#searchBox').val("");
		$.ajax({  
        type: "GET",  
        url: "get-history.do?fundID="+fundID,  
        dataType: "xml",  
        success: historyPro  
	     });
	return false;
}
function transactionPro(xml){
	
	// load data
	tranArr=new Array();
	var index_load=0;
	$(xml).find('transaction').each(function(){
		var id=$(this).find('id').text();
		var price=$(this).find('price').text();
		var share=$(this).find('share').text();
		var amount=$(this).find('amount').text();
		var status=$(this).find('status').text();
		var fund_name=$(this).find('fund_name').text();
		var type=$(this).find('type').text();
		var date=$(this).find('date').text();
		var color=$(this).find('color').text();
		
		var tran=new tranBean(id,price,share,amount,status,fund_name,type,date,color);
		tranArr[index_load]=tran;
		
		index_load++;
	});
	
	// display
	showTran();
}

function showTran(){
	$('#container').html("");
	var len=tranArr.length;
	for(var index=0;index<len;index++){
		var tran=tranArr[index];
		var classType="";
		if(tran.status=="Pending"){
			classType="italic";
		}
		var html="<tr class=\""+classType+"\"><td style=\"width:1px; background-color:#"+tran.color+"\"></td>";
		html+="<td>"+tran.status+"</td>";
		html+="<td>"+tran.id+"</td>";
		html+="<td>"+tran.fund_name+"</td>";
		html+="<td>"+tran.date+"</td>";
		html+="<td>"+tran.share+"</td>";
		html+="<td>"+tran.price+"</td>";
		html+="<td>"+tran.type+"</td>";
		html+="<td>"+tran.amount+"</td>";
		$('#container').append(html);
	}
}

// sort
function statusSort(){
tranArr.sort(compare_status);
statusB=-1*statusB;
showTran();
return false;
}

function idSort(){
	tranArr.sort(compare_id);
	idB=-1*idB;
	showTran();
	return false;
}

function fund_nameSort(){
	tranArr.sort(compare_fund_name);
	fund_nameB=-1*fund_nameB;
	showTran();
	return false;
}

function dateSort(){
	tranArr.sort(compare_date);
	dateB=-1*dateB;
	showTran();
	return false;
}

function shareSort(){
	tranArr.sort(compare_share);
	shareB=-1*shareB;
	showTran();
	return false;
}

function priceSort(){
	tranArr.sort(compare_price);
	priceB=-1*priceB;
	showTran();
	return false;
}

function typeSort(){
	tranArr.sort(compare_type);
	typeB=-1*typeB;
	showTran();
	return false;
}

function amountSort(){
	tranArr.sort(compare_amount);
	amountB=-1*amountB;
	showTran();
	return false;
}

// comparator

function compare_status(a,b){	
	if(a.status>b.status){
		return statusB;
	}
	else{
		return -statusB;
	}
}

function compare_id(a,b){	
	return idB*(parseInt(a.id)-parseInt(b.id));
}

function compare_fund_name(a,b){	
	if(a.fund_name>b.fund_name){
		return fund_nameB;
	}
	else{
		return -fund_nameB;
	}
}

function compare_price(a,b){
	if(a.price==null||a.price.length==0)
		return -priceB;
	if(b.price==null||b.price.length==0)
		return priceB;
	return priceB*(parseFloat(strPro(a.price))-parseFloat(strPro(b.price)));
}

function compare_type(a,b){	
	if(a.type>b.type){
		return typeB;
	}
	else{
		return -typeB;
	}
}

function compare_share(a,b){	
	if(a.share==null||a.share.length==0)
		return -shareB;
	if(b.share==null||b.share.length==0)
		return shareB;
	return shareB*(parseFloat(a.share)-parseFloat(b.share));
}

function compare_amount(a,b){
	if(a.amount==null||a.amount.length==0)
		return amountB;
	if(b.amount==null||b.amount.length==0)
		return -amountB;
	return amountB*(parseFloat(strPro(a.amount))-parseFloat(strPro(b.amount)));
}

function compare_date(a,b){
	if(a.date==null||a.date.length==0){
		return dateB;
	}
	if(b.date==null||b.date.length==0){
		return -dateB;
	}
	var aArr=a.date.split("-");
	var bArr=b.date.split("-");
	var da=new Date(parseInt(aArr[0]),parseInt(aArr[1]),parseInt(aArr[2]));
	var db=new Date(parseInt(bArr[0]),parseInt(bArr[1]),parseInt(bArr[2]));
	if(da>db){
		return dateB;
	}
	else{
		return -dateB;
	}
}

function strPro(str){
	return str.substring(1,str.length);
}